package com.uchedao.seller.ui.carlist.fragment;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.uchedao.seller.R;
import com.uchedao.seller.ui.carlist.BaseFragForBaidu;
import com.uchedao.seller.ui.carlist.adapter.SeriesAdapter;
import com.uchedao.seller.ui.carlist.db.CarDbHelper;
import com.uchedao.seller.ui.carlist.entity.SeriesEntity;

import java.util.ArrayList;

/**
 * Created by ZhangZhanghua on 2015/9/14 0014.
 */
public class FragmentSeries extends BaseFragForBaidu {
    private ListView seriesListView;
    private ArrayList<SeriesEntity> seriesList;
    private SeriesAdapter adapter;
    private String brandName;
    private int brandId;
    private ISeriesClickListener listener;
    @Override
    protected String getActivityTag() {
        return "FragmentSeries";
    }

    @Override
    public int getViewLayoutId() {
        return R.layout.fragment_series;
    }


    @Override
    public void initView() {
        seriesListView = (ListView) findViewById(R.id.series);

    }

    @Override
    public void initData() {
        brandId =  getArguments().getInt("carBrandId");
        seriesList = CarDbHelper.getCarSeries(getActivity(), brandId);
        adapter = new SeriesAdapter(getActivity() , seriesList);
        seriesListView.setAdapter(adapter);
    }

    @Override
    public void initListener() {

        seriesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int sId = ((SeriesEntity) adapter.getItem(position)).getId();
                String sName = ((SeriesEntity) adapter.getItem(position)).getName();
                if (sId == -1){
                    getActivity().finish();
                }
                listener.onSeriesClick(sId, sName);
            }
        });
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try{
            listener =(ISeriesClickListener)activity;
        }catch(ClassCastException e){
            throw new ClassCastException(activity.toString()+"must implement OnArticleSelectedListener");
        }
    }

    public interface ISeriesClickListener
    {
        void onSeriesClick(int sId, String sName);
    }

    @Override
    public void onActivityResultFragment(int requestCode, int resultCode, Intent data) {

    }
}
